package com.ashley.apptools.item;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.ashley.apptools.R;
import com.ashley.apptools.interfaces.IActivity;
import com.ashley.apptools.provider.ToolsData.ItemNumber;
import com.ashley.apptools.utils.ItemNoContentResolver;
import com.ashley.apptools.utils.PriceCount;
import com.ashley.apptools.view.GridViewDrag;
import com.ashley.apptools.view.GridViewDrag.DropListener;
import com.ashley.apptools.vo.ItemVo;
import com.ashley.apptools.vo.MyAdapter;

public class ItemGroupList implements IActivity{

	private Activity mContext;
	private GridViewDrag gridView;
	private ListView listView;
	private MyAdapter mAdapter;
	private ScrollView scrollView;
	
	public ItemGroupList(Activity activity) {
		// TODO Auto-generated constructor stub
		mContext = activity;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		gridView = (GridViewDrag) mContext.findViewById(R.id.inteList);
		listView = (ListView) mContext.findViewById(R.id.listView1);
		scrollView  = (ScrollView) mContext.findViewById(R.id.scrollView1);
		if(mAdapter == null)
		{
			mAdapter = new MyAdapter(mContext, new ArrayList<ItemVo>());
		
			mAdapter.registerDataSetObserver(new DataSetObserver() {
				@Override
				public void onChanged() {
					
					// TODO Auto-generated method stub
					Object[] strings = new Object[]{PriceCount.retailGroupPrice(mAdapter.data()),PriceCount.getItemName(mAdapter.data())};
					handler.obtainMessage(1, strings).sendToTarget();
					super.onChanged();
				}
			});	
		}
		listView.setAdapter(mAdapter);
		gridView.setDropListener(listener);
		gridView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction() == MotionEvent.ACTION_DOWN)
				{
					DisallowInterceptTouchEvent(true);
				}
				return false;
			}
		});
		getItemList();
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRestart() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
	}
	private void getItemList()
	{
		new Thread()
		{
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Cursor cursor = ItemNoContentResolver.getItemNo(mContext.getContentResolver(), ItemNoContentResolver.MEDIUM_DATA, ItemNoContentResolver.TIME_SORT_ORDER);
				handler.obtainMessage(0, cursor).sendToTarget();
				super.run();
			}
		}.start();
		
	}
	private DropListener listener = new DropListener() {
		
		@Override
		public void onDrop(int dragPosition, int vx, int vy) {
			// TODO Auto-generated method stub
			if(gridView.isDragTarget(listView))
			{
				Cursor cursor = (Cursor) gridView.getItemAtPosition(dragPosition);
				ItemVo itemVo = ItemVo.getItemVo(cursor, dragPosition);
				if(itemVo !=null)
				{
					mAdapter.data().add(itemVo);
					mAdapter.notifyDataSetChanged();
				
				}
				
				DisallowInterceptTouchEvent(false);
			}
		}
		
		@Override
		public void onDrag(int dragPosition, int vx, int vy) {
			// TODO Auto-generated me stub
			DisallowInterceptTouchEvent(true);
		}
	};
	
	protected Handler handler = new Handler()
	{
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				Cursor cursor = (Cursor) msg.obj;
				@SuppressWarnings("deprecation")
				SimpleCursorAdapter adapter = new SimpleCursorAdapter(mContext,
						  R.layout.griditem,
						  cursor, 
						  new String[]{ItemNumber.COLUME_ITEM_NO,ItemNumber.COLUME_FREIGHT,ItemNumber.COLUME_TAX},
						  new int[]{R.id.item_no,R.id.freihgt_item,R.id.tax_item});
				gridView.setAdapter(adapter);
				break;

			case 1:
				TextView items = (TextView) mContext.findViewById(R.id.item_grp);
				TextView prices = (TextView) mContext.findViewById(R.id.price_grp);
				Object[] strings = (Object[]) msg.obj;
				items.setText((CharSequence) strings[1]);
				
				prices.setText("Retail: $"+new DecimalFormat("0.00").format(strings[0]));
				break;
			}
		};
	};
	protected void DisallowInterceptTouchEvent(boolean b) {
		// TODO Auto-generated method stub
		if(scrollView !=null)
		{
			scrollView.requestDisallowInterceptTouchEvent(b);
		}
	};
}
